import apis from "../services/messageListApi";

export default {
  namespace: "messageList",
  state: {
    messageList: [],
    loading: false,
    offLineErr: -1,
    offLineErrMsg: "",
    deptList: [],
    listTotal: 0
  },
  effects: {
    // 推送数量趋势图
    *messageList({ payload }, { call, put }) {
      yield put({
        type: "put/loading",
        payload: true
      });
      const res = yield call(apis.messageList, payload);
      const { datas = [], totalCount } = res;
      yield put({
        type: "put/putlist",
        payload: datas
      });
      yield put({
        type: "put/totalCount",
        payload: totalCount
      });
      yield put({
        type: "put/loading",
        payload: false
      });
    },
    *putOffline({ payload }, { call, put }) {
      const res = yield call(apis.putOffline, payload);
      const { errCode, errMsg } = res;
      yield put({
        type: "put/offlineErr",
        payload: errCode
      });
      yield put({
        type: "put/offlineErrMsg",
        payload: errMsg
      });
    },
    *updateStatus({ payload }, { call }) {
      return yield call(apis.updateStatus, payload);
    },
    *delMessage({ payload }, { call }) {
      const res = yield call(apis.delMessage, payload);
      return res;
    },
    *messageDept({ payload }, { call, put }) {
      const res = yield call(apis.messageDept, payload);
      const { datas = [] } = res;
      const deptFilter = datas.map(item => ({
        text: item.ownerDeptName,
        value: item.ownerDept
      }));
      yield put({
        type: "put/putDept",
        payload: deptFilter
      });
    },
    *myMessage({ payload }, { call }) {
      const res = yield call(apis.myMsgs, payload);
      return res;
    }
  },
  reducers: {
    "put/putlist": (state, { payload }) => ({
      ...state,
      messageList: payload
    }),
    "put/loading": (state, { payload }) => ({
      ...state,
      loading: payload
    }),
    "put/offlineErr": (state, { payload }) => ({
      ...state,
      offLineErr: payload
    }),
    "put/offlineErrMsg": (state, { payload }) => ({
      ...state,
      offLineErrMsg: payload
    }),
    "put/putDept": (state, { payload }) => ({
      ...state,
      deptList: payload
    }),
    "put/totalCount": (state, { payload }) => ({
      ...state,
      listTotal: payload
    })
  }
};
